#include "bodypart_rigid_trianglemesh.h"

BodypartRigidTrianglemesh::BodypartRigidTrianglemesh(btVector3* gVertices, unsigned int numVertices, int* gVIndices, unsigned int numVIndices, boost::shared_ptr<btDynamicsWorld> ownerWorld, void* owner, float weight, btVector3& localInertia, const btTransform& transform, const BeColor& clr, float lin_damping, float ang_damping)
  : BodypartRigid( TRIANGLEMESH, clr, ownerWorld )
{
	
	trianglemodel.reset( new btTriangleIndexVertexArray(numVIndices/3, gVIndices, 3*sizeof(int), numVertices/3,(btScalar*) &gVertices[0].x(),3*sizeof(btScalar)) );

// 	trianglemodel = new btTriangleIndexVertexArray(numVIndices/3, gVIndices, 3*sizeof(int), numVertices/3,(btScalar*) &gVertices[0].x(),3*sizeof(btScalar));

	// non rigid: 
// 	btGImpactMeshShape* tshape = new btGImpactMeshShape(trianglemodel,false,btVector3(-100000,-100000,-100000),btVector3(100000,100000,100000));
// 	btGImpactMeshShape * tshape = new btGImpactMeshShape(trianglemodel.get());
// 	tshape->setLocalScaling(btVector3(0.f,150.f,150.f));
// 	tshape->updateBound();
// 	shape = tshape;

	shape = new btBvhTriangleMeshShape(trianglemodel.get(),true);

	// FIXME weight above -> density from material & calculate weight
	generalSetup( localInertia, weight, transform, owner, lin_damping, ang_damping );
// body->setLinearFactor(btVector3(1,1,1));
// body->setAngularFactor(btVector3(1,1,1));

}

BodypartRigidTrianglemesh::BodypartRigidTrianglemesh(boost::shared_ptr<Model> model, boost::shared_ptr<btDynamicsWorld> ownerWorld, void* owner, const btVector3& scale, float weight, btVector3& localInertia, const btTransform& transform, const BeColor& clr, float lin_damping, float ang_damping)
  : BodypartRigid( TRIANGLEMESH, clr, ownerWorld )
{
// 	boost::shared_ptr<nClient> networkclient(m_netServer->getNewConnections( Timer::Instance()->sdl_now ));

	trianglemodel.reset( new btTriangleIndexVertexArray( model->getNumberVIndices()/3, model->indices.get(), 3*sizeof(int), model->getNumberVertices()/3, model->vertices.get(), 3*sizeof(btScalar) ) );
// 	trianglemodel = new btTriangleIndexVertexArray( model->getNumberVIndices()/3, model->indices.get(), 3*sizeof(int), model->getNumberVertices()/3, model->vertices.get(), 3*sizeof(btScalar) );
	
// 	trianglemodel = new btTriangleIndexVertexArray( model->numVIndices/3, model->indices.get(), 3*sizeof(int), model->numVertices/3, model->vertices.get(), 3*sizeof(btScalar) );
// 	trianglemodel = new btTriangleIndexVertexArray(obj->numVIndices/3, obj->gVIndices, 3*sizeof(int), obj->numVertices,(btScalar*) &obj->gVertices[0].x(),sizeof(btVector3));
	shape = new btBvhTriangleMeshShape(trianglemodel.get(),true);

// 	btVector3(-100000,-100000,-100000),btVector3(100000,100000,100000)
// 	btGImpactMeshShape * tshape = new btGImpactMeshShape(trianglemodel);
// 	tshape->updateBound();
// 	float scale = 1000.0f;
// 	tshape->setLocalScaling(btVector3(20,20,20));
	
	// FIXME weight above -> density from material & calculate weight
	generalSetup( localInertia, weight, transform, owner, lin_damping, ang_damping );

// 	m_body->setFriction(0.0f);
// 	body->setRestitution(1000.5f);

// 	model = Modelstore::Instance()->load(obj);
}

BodypartRigidTrianglemesh::~BodypartRigidTrianglemesh()
{
// 	glDeleteBuffersARB(1, &vid);
// 	glDeleteBuffersARB(1, &iid);
// 	delete trianglemodel;
// 	delete serializer;
}
